import 'dart:convert';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_aygsh/util/Toast.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../base/Constant.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_aygsh/http/Api.dart';
import '../../util/AppSaveInfoUtil.dart';
import '../../util/SignUtil.dart';
import '../../base/application.dart';
import '../../http/HttpUtil.dart';
import 'package:cached_network_image/cached_network_image.dart';
import './PointList.dart';
import 'package:flutter_aygsh/liz_mine/set/InviteFriend.dart';
import 'package:flutter_aygsh/liz_mine/order/OrderPage.dart';
import '../../liz_home/pinpai/pinpai.dart';

// PointListPage
class MySignPage extends StatefulWidget {
  MySignPage({Key key}) : super(key: key);

  @override
  _MySignPageState createState() => _MySignPageState();
}

class _MySignPageState extends State<MySignPage> {
  String avatar;
  String token = "";
  String point = "0";
  int continuous_day = 0;
  String signtip = '去签到';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
          title: Text('每日签到',
              style: TextStyle(
                  fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                  color: Colors.black)),
          backgroundColor: Colors.white,
          brightness: Brightness.light,
          centerTitle: true,
          iconTheme: new IconThemeData(color: Colors.black),
          elevation: 0,
          actions: <Widget>[
            GestureDetector(
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.fromLTRB(0, 0, 13, 0),
                child: Text(
                  '积分',
                  style: TextStyle(
                      fontSize:
                          ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                      color: Colors.black),
                ),
              ),
              onTap: () {
                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
                  return new PointListPage();
                }));
              },
            )
          ],
        ),
        body: Container(
            color: f0Color,
            child: Stack(children: <Widget>[
              ClipPath(
                  // 只裁切底部的方法
                  // clipper: BottonClipper(),
                  child: Container(
                color: Color.fromRGBO(39, 45, 63, 1),
                height: ScreenUtil.getInstance().setWidth(DP_200) * 2,
              )),
              CustomScrollView(shrinkWrap: true, slivers: <Widget>[
                SliverToBoxAdapter(
                    child: Container(
                  margin: EdgeInsets.fromLTRB(
                      kMargin30, kMargin30, kMargin20, kMargin30),
                  child: Column(
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          Expanded(
                            child: Container(
                              alignment: Alignment.centerLeft,
                              child: Column(
                                children: <Widget>[
                                  Container(
                                    child: Text(
                                      '我的积分',
                                      style: TextStyle(color: white),
                                    ),
                                  ),
                                  Container(
                                      child: Text(
                                    point,
                                    style: TextStyle(
                                        color: white,
                                        fontSize:
                                            ScreenUtil.getInstance().setSp(60)),
                                  ))
                                ],
                              ),
                            ),
                          ),
                          Container(
                            child: GestureDetector(
                              child: ClipOval(
                                  child: avatar == null || avatar == ''
                                      ? Image.asset(
                                          "images/logo.png",
                                          fit: BoxFit.cover,
                                          width: ScreenUtil.getInstance()
                                              .setWidth(DP_150),
                                          height: ScreenUtil.getInstance()
                                              .setWidth(DP_150),
                                        )
                                      : CachedNetworkImage(
                                          width: ScreenUtil.getInstance()
                                              .setWidth(DP_150),
                                          height: ScreenUtil.getInstance()
                                              .setWidth(DP_150),
                                          fit: BoxFit.cover,
                                          imageUrl: avatar,
                                          placeholder: (context, url) =>
                                              Image.asset(
                                                  'images/pic_default.png',
                                                  width:
                                                      ScreenUtil.getInstance()
                                                          .setWidth(DP_150),
                                                  height:
                                                      ScreenUtil.getInstance()
                                                          .setWidth(DP_150)),
                                          errorWidget: (context, url, error) =>
                                              Image.asset(
                                                  'images/pic_default.png',
                                                  width:
                                                      ScreenUtil.getInstance()
                                                          .setWidth(DP_150),
                                                  height:
                                                      ScreenUtil.getInstance()
                                                          .setWidth(DP_150)),
                                        )),
                              onTap: () {},
                            ),
                          )
                        ],
                      ),
                      Container(
                        padding: EdgeInsets.all(10),
                      ),
                      Container(
                        alignment: Alignment.centerLeft,
                        width: ScreenUtil.getInstance().setWidth(750),
                        decoration: BoxDecoration(
                            color: white,
                            border: Border.all(color: Colors.white, width: 1),
                            borderRadius: BorderRadius.circular(DP_10)),
                        padding: EdgeInsets.all(15),
                        child: Column(
                          children: <Widget>[
                            Container(
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(bottom: kMargin10),
                              child: Text(
                                  '已连续签到' + continuous_day.toString() + '天'),
                            ),
                            Container(
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(bottom: kMargin15),
                              child: Text('连续签到满7天，可领取体验店主'),
                            ),
                            Container(
                              padding: EdgeInsets.all(10),
                              alignment: Alignment.center,
                              child: Row(
                                children: <Widget>[
                                  ClipOval(
                                      child: Container(
                                    alignment: Alignment.center,
                                    width:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    height:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      image: continuous_day >= 1
                                          ? DecorationImage(
                                              image: AssetImage(
                                                  'images/qiandao_bg.png'),
                                              fit: BoxFit.cover)
                                          : DecorationImage(
                                              image: AssetImage(
                                                  'images/touxiang_bg.png'),
                                              fit: BoxFit.cover),
                                    ),
                                    child: Text(
                                      '+5',
                                      style: TextStyle(
                                          color: continuous_day >= 1
                                              ? white
                                              : black),
                                    ),
                                  )),
                                  Expanded(
                                      child: Container(
                                    width: 5,
                                  )),
                                  ClipOval(
                                      child: Container(
                                    alignment: Alignment.center,
                                    width:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    height:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      image: continuous_day >= 2
                                          ? DecorationImage(
                                              image: AssetImage(
                                                  'images/qiandao_bg.png'),
                                              fit: BoxFit.cover)
                                          : DecorationImage(
                                              image: AssetImage(
                                                  'images/touxiang_bg.png'),
                                              fit: BoxFit.cover),
                                    ),
                                    child: Text(
                                      '+10',
                                      style: TextStyle(
                                          color: continuous_day >= 2
                                              ? white
                                              : black),
                                    ),
                                  )),
                                  Expanded(
                                      child: Container(
                                    width: 5,
                                  )),
                                  ClipOval(
                                      child: Container(
                                    alignment: Alignment.center,
                                    width:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    height:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      image: continuous_day >= 3
                                          ? DecorationImage(
                                              image: AssetImage(
                                                  'images/qiandao_bg.png'),
                                              fit: BoxFit.cover)
                                          : DecorationImage(
                                              image: AssetImage(
                                                  'images/touxiang_bg.png'),
                                              fit: BoxFit.cover),
                                    ),
                                    child: Text(
                                      '+15',
                                      style: TextStyle(
                                          color: continuous_day >= 3
                                              ? white
                                              : black),
                                    ),
                                  )),
                                  Expanded(
                                      child: Container(
                                    width: 5,
                                  )),
                                  ClipOval(
                                      child: Container(
                                    alignment: Alignment.center,
                                    width:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    height:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      image: DecorationImage(
                                          image: AssetImage(
                                              'images/touxiang_bg.png'),
                                          fit: BoxFit.cover),
                                    ),
                                    child: Text('+20'),
                                  )),
                                  Expanded(
                                      child: Container(
                                    width: 5,
                                  )),
                                  ClipOval(
                                      child: Container(
                                    alignment: Alignment.center,
                                    width:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    height:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      image: continuous_day >= 4
                                          ? DecorationImage(
                                              image: AssetImage(
                                                  'images/qiandao_bg.png'),
                                              fit: BoxFit.cover)
                                          : DecorationImage(
                                              image: AssetImage(
                                                  'images/touxiang_bg.png'),
                                              fit: BoxFit.cover),
                                    ),
                                    child: Text(
                                      '+25',
                                      style: TextStyle(
                                          color: continuous_day >= 4
                                              ? white
                                              : black),
                                    ),
                                  )),
                                  Expanded(
                                      child: Container(
                                    width: 5,
                                  )),
                                  ClipOval(
                                      child: Container(
                                    alignment: Alignment.center,
                                    width:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    height:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      image: continuous_day >= 5
                                          ? DecorationImage(
                                              image: AssetImage(
                                                  'images/qiandao_bg.png'),
                                              fit: BoxFit.cover)
                                          : DecorationImage(
                                              image: AssetImage(
                                                  'images/touxiang_bg.png'),
                                              fit: BoxFit.cover),
                                    ),
                                    child: Text(
                                      '+30',
                                      style: TextStyle(
                                          color: continuous_day >= 5
                                              ? white
                                              : black),
                                    ),
                                  )),
                                  Expanded(
                                      child: Container(
                                    width: 5,
                                  )),
                                  ClipOval(
                                      child: Container(
                                    alignment: Alignment.center,
                                    width:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    height:
                                        ScreenUtil.getInstance().setWidth(750) /
                                            9,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      image: continuous_day >= 6
                                          ? DecorationImage(
                                              image: AssetImage(
                                                  'images/qiandao_bg.png'),
                                              fit: BoxFit.cover)
                                          : DecorationImage(
                                              image: AssetImage(
                                                  'images/touxiang_bg.png'),
                                              fit: BoxFit.cover),
                                    ),
                                    child: Text(
                                      '+50',
                                      style: TextStyle(
                                          color: continuous_day >= 6
                                              ? white
                                              : black),
                                    ),
                                  )),
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        height: 10,
                      ),
                      Container(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '积分奖励',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance().setSp(30),
                              fontWeight: FontWeight.w500),
                        ),
                      ),
                      Container(
                        height: 10,
                      ),
                      Container(
                        decoration: BoxDecoration(
                            color: white,
                            border: Border.all(color: Colors.white, width: 1),
                            borderRadius: BorderRadius.circular(DP_10)),
                        padding: EdgeInsets.all(kMargin30),
                        child: Column(
                          children: <Widget>[
                            Row(
                              children: <Widget>[
                                Image.asset('images/jifen_qiandao.png'),
                                Expanded(
                                    child: Container(
                                        margin: EdgeInsets.only(left: 5),
                                        alignment: Alignment.centerLeft,
                                        child: Column(
                                          children: <Widget>[
                                            Container(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                '每日签到',
                                                style: TextStyle(
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(30),
                                                    fontWeight:
                                                        FontWeight.w500),
                                              ),
                                            ),
                                            Container(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                '连续签到可获得10积分',
                                                style: TextStyle(
                                                    color: Colors.grey,
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(25)),
                                              ),
                                            ),
                                          ],
                                        ))),
                                GestureDetector(
                                  onTap: () {
                                    sign();
                                  },
                                  child: Container(
                                    decoration: BoxDecoration(
                                        color: Colors.red,
                                        border: Border.all(
                                            color: Colors.red, width: 1),
                                        borderRadius:
                                            BorderRadius.circular(DP_20)),
                                    alignment: Alignment.center,
                                    padding: EdgeInsets.all(5),
                                    width:
                                        ScreenUtil.getInstance().setWidth(120),
                                    child: Text(
                                      signtip,
                                      maxLines: 1,
                                      style: TextStyle(
                                          color: white,
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(25)),
                                    ),
                                  ),
                                )
                              ],
                            ),
                            Container(
                              height: 20,
                            ),
                            Row(
                              children: <Widget>[
                                Image.asset('images/jifen_xiadan.png'),
                                Expanded(
                                    child: Container(
                                        margin: EdgeInsets.only(left: 5),
                                        alignment: Alignment.centerLeft,
                                        child: Column(
                                          children: <Widget>[
                                            Container(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                '下单成功',
                                                style: TextStyle(
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(30),
                                                    fontWeight:
                                                        FontWeight.w500),
                                              ),
                                            ),
                                            Container(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                '订单完成后可获得积分',
                                                style: TextStyle(
                                                    color: Colors.grey,
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(25)),
                                              ),
                                            ),
                                          ],
                                        ))),
                                GestureDetector(
                                  onTap: () {
                                    Navigator.of(context).push(
                                        new MaterialPageRoute(
                                            builder: (context) {
                                      return PINPAIPage();
                                    }));
                                  },
                                  child: Container(
                                    decoration: BoxDecoration(
                                        color: Colors.red,
                                        border: Border.all(
                                            color: Colors.red, width: 1),
                                        borderRadius:
                                            BorderRadius.circular(DP_20)),
                                    alignment: Alignment.center,
                                    padding: EdgeInsets.all(5),
                                    width:
                                        ScreenUtil.getInstance().setWidth(120),
                                    child: Text(
                                      '去逛逛',
                                      maxLines: 1,
                                      style: TextStyle(
                                          color: white,
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(25)),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            Container(
                              height: 20,
                            ),
                            Row(
                              children: <Widget>[
                                Image.asset('images/jifen_pingjia.png'),
                                Expanded(
                                    child: Container(
                                        margin: EdgeInsets.only(left: 5),
                                        alignment: Alignment.centerLeft,
                                        child: Column(
                                          children: <Widget>[
                                            Container(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                '评级晒单',
                                                style: TextStyle(
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(30),
                                                    fontWeight:
                                                        FontWeight.w500),
                                              ),
                                            ),
                                            Container(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                '20字以上优质评价',
                                                style: TextStyle(
                                                    color: Colors.grey,
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(25)),
                                              ),
                                            ),
                                          ],
                                        ))),
                                GestureDetector(
                                  onTap: () {
                                    Navigator.of(context).push(
                                        new MaterialPageRoute(
                                            builder: (context) {
                                      return new OrderPage(index: 4);
                                    }));
                                  },
                                  child: Container(
                                    decoration: BoxDecoration(
                                        color: Colors.red,
                                        border: Border.all(
                                            color: Colors.red, width: 1),
                                        borderRadius:
                                            BorderRadius.circular(DP_20)),
                                    alignment: Alignment.center,
                                    padding: EdgeInsets.all(5),
                                    width:
                                        ScreenUtil.getInstance().setWidth(120),
                                    child: Text(
                                      '去评价',
                                      maxLines: 1,
                                      style: TextStyle(
                                          color: white,
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(25)),
                                    ),
                                  ),
                                )
                              ],
                            ),
                            Container(
                              height: 20,
                            ),
                            Row(
                              children: <Widget>[
                                Image.asset('images/jifen_yaoqing.png'),
                                Expanded(
                                    child: Container(
                                        margin: EdgeInsets.only(left: 5),
                                        alignment: Alignment.centerLeft,
                                        child: Column(
                                          children: <Widget>[
                                            Container(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                '邀请好友注册',
                                                style: TextStyle(
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(30),
                                                    fontWeight:
                                                        FontWeight.w500),
                                              ),
                                            ),
                                            Container(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                '推荐成功一位可获200积分',
                                                style: TextStyle(
                                                    color: Colors.grey,
                                                    fontSize:
                                                        ScreenUtil.getInstance()
                                                            .setSp(25)),
                                              ),
                                            ),
                                          ],
                                        ))),
                                GestureDetector(
                                  onTap: () {
                                    //邀请好友
                                    Navigator.of(context).push(
                                        new MaterialPageRoute(
                                            builder: (context) {
                                      return new InviteFriendPage();
                                    }));
                                  },
                                  child: Container(
                                    decoration: BoxDecoration(
                                        color: Colors.red,
                                        border: Border.all(
                                            color: Colors.red, width: 1),
                                        borderRadius:
                                            BorderRadius.circular(DP_20)),
                                    alignment: Alignment.center,
                                    padding: EdgeInsets.all(5),
                                    width:
                                        ScreenUtil.getInstance().setWidth(120),
                                    child: Text(
                                      '去邀请',
                                      maxLines: 1,
                                      style: TextStyle(
                                          color: white,
                                          fontSize: ScreenUtil.getInstance()
                                              .setSp(25)),
                                    ),
                                  ),
                                )
                              ],
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                )),
              ])
            ])));
  }

  @override
  void initState() {
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
      getUserInfo();
      getSignDay();
      is_Sign();
    });
  }

  getUserInfo() async {
    Map map = new Map();
    map['token'] = token;
    map['sign_type'] = createSignType(Api.URL_USER_GETMSG);
    print(map);
    Response<String> response =
        await HttpUtil().post(Api.URL_USER_GETMSG, data: getSign(map));
    Map userMap = json.decode(response.data);
    print(userMap);
    if (userMap['code'] == 1 && mounted) {
      setState(() {
        APPLIC.userInfoMap = userMap['data'];
        userMap = userMap['data'];
        // user_msg = userMap['user_msg'];
        // user_detail = userMap['user_detail'];
        point = userMap['user_msg']['point'].toString();
        avatar = userMap['user_detail']['avatar'];
      });
    }
  }

  sign() async {
    //签到
    Map map = new Map();
    map['token'] = token;
    map['sign_type'] = createSignType(Api.URL_SIGN_SINGIN);
    print(map);
    Response<String> response =
        await HttpUtil().post(Api.URL_SIGN_SINGIN, data: getSign(map));
    Map userMap = json.decode(response.data);
    print(userMap);
    if (userMap['code'] == 1 && mounted) {
      setState(() {
         is_Sign();
        getSignDay();
        getUserInfo();
      });
    }
    Toast.toast(context,msg:userMap['msg']);
  }

  is_Sign() async {
    //今日是否签到
    Map map = new Map();
    map['token'] = token;
    map['sign_type'] = createSignType(Api.URL_SIGN_ISSIGNTODAY);
    Response<String> response =
        await HttpUtil().post(Api.URL_SIGN_ISSIGNTODAY, data: getSign(map));
    Map userMap = json.decode(response.data);
    print(userMap);
    if (userMap['code'] == 1 && mounted) {
      if (userMap['data']['is_sign'] == 'Y') {
        setState(() {
          signtip = '已签到';
        });
      } else {
        signtip = '去签到';
      }
    }
  }

  getSignDay() async {
    //获取用户连续签到天数
    Map map = new Map();
    map['token'] = token;
    map['sign_type'] = createSignType(Api.URL_SIGN_CONTINUOUSDAY);
    print(map);
    Response<String> response =
        await HttpUtil().post(Api.URL_SIGN_CONTINUOUSDAY, data: getSign(map));
    Map userMap = json.decode(response.data);
    print(userMap);
    if (userMap['code'] == 1 && mounted) {
      setState(() {
        continuous_day = userMap['data']['continuous_day'];
      });
    }
  }
}
